<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="dataForm.orderNo" placeholder="订单编号"></el-input>
      </el-form-item>
          <el-form-item label="航空公司二字码" prop="airline">
          <el-input v-model="dataForm.airline" placeholder="航空公司二字码"></el-input>
      </el-form-item>
          <el-form-item label="航空公司" prop="airlineName">
          <el-input v-model="dataForm.airlineName" placeholder="航空公司"></el-input>
      </el-form-item>
          <el-form-item label="适用班期类型" prop="applyFlightType">
          <el-input v-model="dataForm.applyFlightType" placeholder="适用班期类型"></el-input>
      </el-form-item>
          <el-form-item label="适用班期类型文本" prop="applyFlightTypeText">
          <el-input v-model="dataForm.applyFlightTypeText" placeholder="适用班期类型文本"></el-input>
      </el-form-item>
          <el-form-item label="舱位" prop="cabin">
          <el-input v-model="dataForm.cabin" placeholder="舱位"></el-input>
      </el-form-item>
          <el-form-item label="航程类型 1-单程 2-往返" prop="flightType">
          <el-input v-model="dataForm.flightType" placeholder="航程类型 1-单程 2-往返"></el-input>
      </el-form-item>
          <el-form-item label="留钱" prop="keepMoney">
          <el-input v-model="dataForm.keepMoney" placeholder="留钱"></el-input>
      </el-form-item>
          <el-form-item label="返点" prop="rebate">
          <el-input v-model="dataForm.rebate" placeholder="返点"></el-input>
      </el-form-item>
          <el-form-item label="销售日期结束" prop="saleEndDate">
          <el-input v-model="dataForm.saleEndDate" placeholder="销售日期结束"></el-input>
      </el-form-item>
          <el-form-item label="销售日期开始" prop="saleStartDate">
          <el-input v-model="dataForm.saleStartDate" placeholder="销售日期开始"></el-input>
      </el-form-item>
          <el-form-item label="快照政策编号" prop="snapshotPolicyNo">
          <el-input v-model="dataForm.snapshotPolicyNo" placeholder="快照政策编号"></el-input>
      </el-form-item>
          <el-form-item label="政策类型0-私有 1-公布 2-特殊 3-官网" prop="snapshotPolicyType">
          <el-input v-model="dataForm.snapshotPolicyType" placeholder="政策类型0-私有 1-公布 2-特殊 3-官网"></el-input>
      </el-form-item>
          <el-form-item label="出票时限方式" prop="ticket">
          <el-input v-model="dataForm.ticket" placeholder="出票时限方式"></el-input>
      </el-form-item>
          <el-form-item label="出票方式" prop="ticketChannels">
          <el-input v-model="dataForm.ticketChannels" placeholder="出票方式"></el-input>
      </el-form-item>
          <el-form-item label="出票方式文本" prop="ticketChannelsText">
          <el-input v-model="dataForm.ticketChannelsText" placeholder="出票方式文本"></el-input>
      </el-form-item>
          <el-form-item label="出票时限方式文本 1-支付后 2-起飞前" prop="ticketText">
          <el-input v-model="dataForm.ticketText" placeholder="出票时限方式文本 1-支付后 2-起飞前"></el-input>
      </el-form-item>
          <el-form-item label="票证类型" prop="ticketType">
          <el-input v-model="dataForm.ticketType" placeholder="票证类型"></el-input>
      </el-form-item>
          <el-form-item label="出票时限" prop="timeLimit">
          <el-input v-model="dataForm.timeLimit" placeholder="出票时限"></el-input>
      </el-form-item>
          <el-form-item label="时间单位" prop="timeUnit">
          <el-input v-model="dataForm.timeUnit" placeholder="时间单位"></el-input>
      </el-form-item>
          <el-form-item label="外部政策id【暂未使用】" prop="externalPolicyId">
          <el-input v-model="dataForm.externalPolicyId" placeholder="外部政策id【暂未使用】"></el-input>
      </el-form-item>
          <el-form-item label="office 号" prop="officeNo">
          <el-input v-model="dataForm.officeNo" placeholder="office 号"></el-input>
      </el-form-item>
          <el-form-item label="政策其他说明" prop="otherContents">
          <el-input v-model="dataForm.otherContents" placeholder="政策其他说明"></el-input>
      </el-form-item>
              <el-form-item label="更新人" prop="updator">
          <el-input v-model="dataForm.updator" placeholder="更新人"></el-input>
      </el-form-item>
          <el-form-item label="更新时间" prop="updateDate">
          <el-input v-model="dataForm.updateDate" placeholder="更新时间"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        orderNo: '',
        airline: '',
        airlineName: '',
        applyFlightType: '',
        applyFlightTypeText: '',
        cabin: '',
        flightType: '',
        keepMoney: '',
        rebate: '',
        saleEndDate: '',
        saleStartDate: '',
        snapshotPolicyNo: '',
        snapshotPolicyType: '',
        ticket: '',
        ticketChannels: '',
        ticketChannelsText: '',
        ticketText: '',
        ticketType: '',
        timeLimit: '',
        timeUnit: '',
        externalPolicyId: '',
        officeNo: '',
        otherContents: '',
        creator: '',
        createDate: '',
        updator: '',
        updateDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        orderNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        airline: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        airlineName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        applyFlightType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        applyFlightTypeText: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        cabin: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        flightType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        keepMoney: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        rebate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        saleEndDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        saleStartDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        snapshotPolicyNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        snapshotPolicyType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ticket: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ticketChannels: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ticketChannelsText: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ticketText: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ticketType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        timeLimit: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        timeUnit: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        externalPolicyId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        officeNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        otherContents: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updator: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/flight/orderpolicy/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/flight/orderpolicy/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
